package com.jwell.panprovider.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;


import com.jwell.panprovider.R;
import com.jwell.panprovider.ui.adapter.ChooseStringAdapter;

import java.util.List;


/**
 * 选择列表的Dialog
 */
public class ChooseStringDialog {
    public static class Builder {
        private Context context;
        private Onclick listener;
        private List<String> stringList;

        public Builder(Context context, List<String> stringList) {
            if (context == null) {
                return;
            }
            this.context = context;
            this.stringList = stringList;
        }

        public Builder setOnclickListener(Onclick listener) {
            this.listener = listener;
            return this;
        }

        public Dialog creat() {
            if (context == null) {
                return null;
//                throw new IllegalArgumentException("Dialog context is null");
            }
            final Dialog dialog = new Dialog(context, 0);
            View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_list_layout, null);
            RecyclerView recyclerView = contentView.findViewById(R.id.recyclerView);
            LinearLayout ll_content = (LinearLayout) contentView.findViewById(R.id.ll_content);
            if (stringList != null && !stringList.isEmpty()) {
                if (stringList.size() > 5) {
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 840);
                    ll_content.setLayoutParams(params);
                } else {
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    ll_content.setLayoutParams(params);
                }
                ChooseStringAdapter adapter = new ChooseStringAdapter(stringList);
                recyclerView.setAdapter(adapter);
                recyclerView.setLayoutManager(new LinearLayoutManager(context));
                adapter.setOnclickListener(new ChooseStringAdapter.OnclickListener() {
                    @Override
                    public void Onclick(int position) {
                        dialog.dismiss();
                        if (listener != null) {
                            listener.onClick(stringList.get(position));
                        }
                    }
                });
            }

            //其他设置
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //设置没有标题
            Window window = dialog.getWindow();
            dialog.setCanceledOnTouchOutside(true);//设置Dialog以外是否可触摸
            window.setGravity(Gravity.BOTTOM);  //此处可以设置dialog显示的位置
            window.setWindowAnimations(R.style.mystyle);  //添加动画
            window.setContentView(contentView);
            WindowManager.LayoutParams windowparams = dialog.getWindow().getAttributes();
            Rect rect = new Rect();
            View view1 = dialog.getWindow().getDecorView();
            view1.getWindowVisibleDisplayFrame(rect);
            windowparams.width = WindowManager.LayoutParams.MATCH_PARENT;
            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            dialog.getWindow().setAttributes((WindowManager.LayoutParams) windowparams);
            return dialog;
        }
    }

    public interface Onclick {
        void onClick(String string);
    }
}
